<?php
include_once(dirname(__FILE__)."/../inc/common.php"); 
$method = isset($_GET["method"])?$_GET["method"]:"";

if($method=="add"){
?>
<div id="winwrapper" class="winwrapper">
<div>
  	<form id="addForm" name="addForm"  method="post">
  			
		<div class="content">
		    <fieldset>
		    	<legend>Caakee账号信息</legend>
		    		<table cellpadding="0" cellspacing="1" class="form_table">
						<tr>
							<td class="caption">登录账号:
							</td>
							<td class="field">
								<input class="text" type="text" name="loginAccount">
							</td>
						</tr>
						<tr>
							<td class="caption">登录密码:
							</td>
							<td class="field">
								<input class="text" type="password" name="loginPassword">
							</td>
						</tr>
				</table>
				
			</fieldset>
		</div>
			
			<div class="buttons">
				<input id="saveBtn" type="button" class="button"  value="开始导入" onclick="ImportAccount.doAdd(this);">
			</div>
			
			<div class="tip_wrapper" style="display:none">
				<a class="colse_btn" onclick="Help.goClose();" href="#" align="absmiddle"><img src="images/icons/icon_close.gif"/></a>
				<span class="title">提示:</span>
				<span class="tip_content"></span>
			</div>
  	
 </form>
</div>
</div>
<?php
}else if($method=="doAdd"){
	$result = array(
		"returnCode" => 1,
		"returnMsg" => "失败",
	);
	if(isset($_POST["loginAccount"]) && $_POST["loginPassword"]){
		try{
			$continue = $_GET["continue"];
			$importClass = new tally_import($userId);

			if(!$continue){
				$importClass->login($_POST["loginAccount"], $_POST["loginPassword"]);
				$importClass->importStart();
				$importStatus = $importClass->getStatus();
				$currentAccount = $importStatus['accountList'][$importStatus['currentAccountInTotal']];
				$totalAccountCount = $importStatus['totalAccountCount'];
				$result = array(
					"returnCode" => 0,
					"backgroundRunning" => 1,
					"returnMsg" => "登录成功,正在导入".$currentAccount["name"]."第1页,进度(1/".$totalAccountCount.")...",
				);
			}else{
				$importClass->importFlows();
				$importStatus = $importClass->getStatus();
				$totalAccountCount = $importStatus['totalAccountCount'];
				$currentAccountInTotal = $importStatus['currentAccountInTotal'];
				if($currentAccountInTotal<$totalAccountCount){
					$currentAccount = $importStatus['accountList'][$importStatus['currentAccountInTotal']];
					$page = $importStatus['page'];
					if($page===null){
						$page = 1;
					}else{
						$page = $importStatus['pageCount']- $page+1;
					}
					$result = array(
						"returnCode" => 0,
						"backgroundRunning" => 1,
						"returnMsg" => "正在导入".$currentAccount["name"]."第".$page."页,进度(".($currentAccountInTotal+1)."/".$totalAccountCount.")...",
					);
				}else{
					$result = array(
						"returnCode" => 0,
						"returnMsg" => "导入完成",
					);
				}
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"导入失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}
?>